package com.hcy.rabbitmanagerapi.controller;

import com.hcy.manager.ManagerDriverCarInfoClient;
import com.hcy.manager.dto.request.*;
import com.hcy.manager.dto.response.*;
import com.hcy.rabbitbasicmodel.dto.MyPage;
import com.hcy.rabbitbasicmodel.enums.Log_Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.model.ReturnResult;
import com.hcy.rabbitbasicmodel.signl.Add;
import com.hcy.rabbitbasicmodel.signl.Delete;
import com.hcy.rabbitbasicmodel.signl.Query;
import com.hcy.rabbitmanagerapi.utils.MyAutoOptLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/4/28
 **/
@RestController
@Slf4j
@RequestMapping("/car")
public class CarController {

    @Autowired
    ManagerDriverCarInfoClient managerDriverCarInfoClient;


    @PostMapping("/getCarList")
    public ReturnResult getCarList(@Validated(Query.class) @RequestBody CarInfoRequestDTO requestDTO) {
        log.info("getCarList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<MyPage<CarInfoResponseDTO>> carList = managerDriverCarInfoClient.getCarList(requestDTO);
            if (carList.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(carList.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/deleteCar")
    @MyAutoOptLog(Log_Code.D_CAR)
    public ReturnResult deleteCar(@Validated(Delete.class) @RequestBody CarInfoRequestDTO requestDTO) {
        log.info("deleteCar start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            requestDTO.setStatus(-1);
            ResponseData<Object> responseData = managerDriverCarInfoClient.updateCarStatus(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }
    @PostMapping("/publicCar")
    @MyAutoOptLog(Log_Code.U_CAR)
    public ReturnResult publicCar(@Validated(Delete.class) @RequestBody CarInfoRequestDTO requestDTO) {
        log.info("publicCar start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            requestDTO.setStatus(1);
            ResponseData<Object> responseData = managerDriverCarInfoClient.updateCarStatus(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/downCar")
    @MyAutoOptLog(Log_Code.U_CAR)
    public ReturnResult downCar(@Validated(Delete.class) @RequestBody CarInfoRequestDTO requestDTO) {
        log.info("downCar start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            requestDTO.setStatus(0);
            ResponseData<Object> responseData = managerDriverCarInfoClient.updateCarStatus(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/insertOrUpdateCar")
    @MyAutoOptLog(Log_Code.U_CAR)
    public ReturnResult insertOrUpdate(@Validated @RequestBody CarInfoAddRequestDTO requestDTO) {
        log.info("insertOrUpdate start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<CarInfoResponseDTO> responseDTOResponseData = managerDriverCarInfoClient.insertOrUpdate(requestDTO);
            if (responseDTOResponseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseDTOResponseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/getCarTypeList")
    public ReturnResult getCarTypeList() {
        log.info("getCarTypeList start");
        try {
            ResponseData<List<CarTypeResponseDTO>> carTypeList = managerDriverCarInfoClient.getCarTypeList();
            if (carTypeList.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(carTypeList.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/insertOrUpdateCarType")
    @MyAutoOptLog(Log_Code.U_CAR_TYPE)
    public ReturnResult insertOrUpdateCarType(@Validated(Add.class) @RequestBody CarTypeRequestDTO requestDTO) {
        log.info("insertOrUpdateCarType start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<CarTypeResponseDTO> responseData = managerDriverCarInfoClient.insertOrUpdateCarType(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/deleteCarType")
    @MyAutoOptLog(Log_Code.D_CAR_TYPE)
    public ReturnResult deleteCarType(@Validated(Delete.class) @RequestBody CarTypeRequestDTO requestDTO) {
        log.info("deleteCarType start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<Object> responseData = managerDriverCarInfoClient.deleteCarType(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/getCarLevelList")
    public ReturnResult getCarLevelList() {
        log.info("getCarLevelList start");
        try {
            ResponseData<List<CarLevelResponseDTO>> carLevelList = managerDriverCarInfoClient.getCarLevelList();
            if (carLevelList.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(carLevelList.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/insertOrUpdateCarLevel")
    @MyAutoOptLog(Log_Code.U_CAR_LEVEL)
    public ReturnResult insertOrUpdateCarLevel(@Validated(Add.class) @RequestBody CarLevelRequestDTO requestDTO) {
        log.info("insertOrUpdateCarLevel start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<CarLevelResponseDTO> responseData = managerDriverCarInfoClient.insertOrUpdateCarLevel(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }

    @PostMapping("/deleteCarLevel")
    @MyAutoOptLog(Log_Code.D_CAR_LEVEL)
    public ReturnResult deleteCarLevel(@Validated(Delete.class) @RequestBody CarLevelRequestDTO requestDTO) {
        log.info("deleteCarLevel start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<Object> responseData = managerDriverCarInfoClient.deleteCarLevel(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("系统繁忙");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("系统繁忙");
            return ReturnResult.fail("系统繁忙");
        }
    }
}
